define(function(require) {

  var $ = require("jquery");
  var _ = require("underscore");
  var Backbone = require("backbone");
  var MyModel = require("models/MyModel");
  var ErrorModel = require("models/ErrorModel");
  var StructureView = require("views/StructureView");
  var StructureLoginView = require("views/StructureLoginView");
  var MyView = require("views/pages/MyView");
  var MapView = require("views/pages/MapView");
  var PaginaRicercaMappa = require("views/pages/PaginaRicercaMappa");
  var InserisciAnnuncio = require("views/pages/InserisciAnnuncio");
  var LoginView = require("views/pages/LoginView");
  var DettagliAnnuncio = require("views/pages/DettagliAnnuncio")
  var LoginFacebookView = require("views/pages/LoginFacebookView");
  var LoginIoRegaloView = require("views/pages/LoginIoRegaloView");
  var HomePageView = require("views/pages/HomePageView");
  var RecuperaDatiView = require("views/pages/RecuperaDatiView");
  var Parse = require("parse");
  var Autocomplete = require("autocomplete");

  var AppRouter = Backbone.Router.extend({

    constructorName: "AppRouter",

    routes: {
      // the default is the structure view
      "": "showStructureLogin",
      "showstructure" : "showStructure",
      "loginFacebook": "loginFacebook",
      "loginIoRegalo" : "loginIoRegalo", 
      "logOut" : "logOut",
      "loginIoRegalo/:urlNumber/:message" : "loginIoRegalo",
      "myview": "myView",
      "map": "map",
      "dettagliannuncio/:id" : "dettagliAnnuncio",
      "homePage" : "homePage",
      "login": "logIn",
      "paginaricercamappa":"searchMapView",
      "inserisciannuncio" : "inserisciAnnuncio",
      "recuperaDati" : "recuperaDati"
    },

    firstView: "login",
    secondView: "homePage",

    initialize: function(options) {
      this.currentView = undefined;
    },

    inserisciAnnuncio: function(){
      
      // create the view
      var page = new InserisciAnnuncio(); 
      // show the view
      this.changePage(page);
      
    },

    searchMapView: function(){
      
      // create the view
      var page = new PaginaRicercaMappa(); 
      // show the view
      this.changePage(page);

    },

    dettagliAnnuncio: function(id){
      var page = new DettagliAnnuncio();
      this.changePage(page);
      page.dettagli(id);
    },

    // load the structure view
    showStructureLogin: function() {
      //alert("sono entrato in showStructure");
      if (!this.structureLoginView) {
        this.structureLoginView = new StructureLoginView();
        // put the el element of the structure view into the DOM
        document.body.appendChild(this.structureLoginView.render().el);
        this.structureLoginView.trigger("inTheDOM");
      }
      // go to first view
      this.navigate(this.firstView, {trigger: true});
    },

    loginFacebook: function(){

      var page = new LoginFacebookView();
      this.myChangePage(page);

    },

    loginIoRegalo: function(urlNumber,message){

        // se ricevo in input un codice d'errore chiamalo ma showMessage dell'errore
        if(urlNumber){

            if(urlNumber == 0) {
              message = "▼ " + message;
              var classe = "alert-danger";
            }    
            else {
              message = "✔ " + message;
              var classe = "alert-success";
            }
 
            var page = new LoginIoRegaloView();
            this.myChangePage(page);
            page.showMessage(message,classe);
          
          } else {
            var page = new LoginIoRegaloView();
            this.myChangePage(page);

          }
          
    },

    logIn: function(){
        
        if(!localStorage.getItem('login')){
            alert("local vuoto");
            //this.showStructure();
      
            var page = new LoginView();
            this.myChangePage(page);

        } else {
            alert("local non vuoto");
            // se esiste un localStorage vuol dire che l'utente già ha effettuato il login
            // quindi vado nella home page
            this.showStructure();
                          
            //var page = new LoginView();
            //this.myChangePage(page);

        }
         
    },

    logOut: function() {

      localStorage.clear();
      this.logIn();
    
    },

    recuperaDati: function(){

      var page = new RecuperaDatiView();
      this.myChangePage(page);
    
    },

    myView: function() {

      // highlight the nav1 tab bar element as the current one
      //this.structureView.setActiveTabBarElement("nav1");
      // create a model with an arbitrary attribute for testing the template engine
      var model = new MyModel({
        key: "testValue"
      });
      // create the view
      var page = new MyView({
        model: model
      });
      // show the view
      this.changePage(page);
    },

    homePage: function(){
      var page = new HomePageView();
      this.changePage(page);
    },

    map: function() {
      // highlight the nav2 tab bar element as the current one
      /* this.structureView.setActiveTabBarElement("nav2"); */
      // create the view and show it
      var page = new MapView();
      this.changePage(page);
    },

    // load the structure view
    showStructure: function() {
      if (!this.structureView) {
        this.structureView = new StructureView();
        // put the el element of the structure view into the DOM
        document.body.appendChild(this.structureView.render().el);
        this.structureView.trigger("inTheDOM");
      }
      // go to first view
      this.navigate(this.secondView, {trigger: true});
    },

  });

  return AppRouter;

});